#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from PySide6.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
    QPushButton, QFrame, QTabWidget, QWidget,
    QSpinBox, QCheckBox, QComboBox, QFormLayout,
    QGroupBox
)
from PySide6.QtGui import QFont, QIcon
from PySide6.QtCore import Qt

class SettingsDialog(QDialog):
    """设置对话框"""
    
    def __init__(self, config_manager, parent=None):
        """初始化设置对话框
        
        Args:
            config_manager: 配置管理器实例
            parent: 父窗口
        """
        super().__init__(parent)
        
        self.config_manager = config_manager
        
        # 设置窗口属性
        self.setWindowTitle("设置")
        self.setWindowIcon(QIcon("app/resources/icons/bunny.png"))
        self.setMinimumSize(500, 400)
        
        # 创建UI组件
        self._create_ui()
        
        # 加载当前配置
        self._load_config()
    
    def _create_ui(self):
        """创建UI组件"""
        # 创建主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(20, 20, 20, 20)
        main_layout.setSpacing(15)
        
        # 创建标签页
        tab_widget = QTabWidget()
        
        # 创建番茄钟设置页
        pomodoro_tab = QWidget()
        tab_widget.addTab(pomodoro_tab, "番茄钟")
        
        # 创建Flowdoro设置页
        flowdoro_tab = QWidget()
        tab_widget.addTab(flowdoro_tab, "Flowdoro")
        
        # 创建通用设置页
        general_tab = QWidget()
        tab_widget.addTab(general_tab, "通用")
        
        # 添加标签页到主布局
        main_layout.addWidget(tab_widget)
        
        # 创建番茄钟设置
        self._create_pomodoro_settings(pomodoro_tab)
        
        # 创建Flowdoro设置
        self._create_flowdoro_settings(flowdoro_tab)
        
        # 创建通用设置
        self._create_general_settings(general_tab)
        
        # 创建按钮布局
        button_layout = QHBoxLayout()
        button_layout.setSpacing(10)
        
        # 创建保存按钮
        self.save_button = QPushButton("保存")
        self.save_button.setMinimumHeight(40)
        self.save_button.clicked.connect(self._on_save_clicked)
        button_layout.addWidget(self.save_button)
        
        # 创建取消按钮
        self.cancel_button = QPushButton("取消")
        self.cancel_button.setMinimumHeight(40)
        self.cancel_button.clicked.connect(self.reject)
        button_layout.addWidget(self.cancel_button)
        
        main_layout.addLayout(button_layout)
    
    def _create_pomodoro_settings(self, parent):
        """创建番茄钟设置
        
        Args:
            parent: 父窗口
        """
        layout = QFormLayout(parent)
        layout.setContentsMargins(20, 20, 20, 20)
        layout.setSpacing(15)
        
        # 工作时长设置
        self.work_minutes_spinbox = QSpinBox()
        self.work_minutes_spinbox.setRange(1, 120)
        self.work_minutes_spinbox.setSuffix(" 分钟")
        layout.addRow("工作时长:", self.work_minutes_spinbox)
        
        # 休息时长设置
        self.break_minutes_spinbox = QSpinBox()
        self.break_minutes_spinbox.setRange(1, 60)
        self.break_minutes_spinbox.setSuffix(" 分钟")
        layout.addRow("休息时长:", self.break_minutes_spinbox)
        
        # 长休息时长设置
        self.long_break_minutes_spinbox = QSpinBox()
        self.long_break_minutes_spinbox.setRange(5, 120)
        self.long_break_minutes_spinbox.setSuffix(" 分钟")
        layout.addRow("长休息时长:", self.long_break_minutes_spinbox)
        
        # 长休息间隔设置
        self.long_break_after_spinbox = QSpinBox()
        self.long_break_after_spinbox.setRange(1, 10)
        self.long_break_after_spinbox.setSuffix(" 个番茄钟")
        layout.addRow("长休息间隔:", self.long_break_after_spinbox)
    
    def _create_flowdoro_settings(self, parent):
        """创建Flowdoro设置
        
        Args:
            parent: 父窗口
        """
        layout = QFormLayout(parent)
        layout.setContentsMargins(20, 20, 20, 20)
        layout.setSpacing(15)
        
        # 休息比例设置
        self.break_ratio_spinbox = QSpinBox()
        self.break_ratio_spinbox.setRange(10, 50)
        self.break_ratio_spinbox.setSuffix(" %")
        layout.addRow("休息时间比例:", self.break_ratio_spinbox)
        
        # 最短休息时间设置
        self.min_break_minutes_spinbox = QSpinBox()
        self.min_break_minutes_spinbox.setRange(1, 30)
        self.min_break_minutes_spinbox.setSuffix(" 分钟")
        layout.addRow("最短休息时间:", self.min_break_minutes_spinbox)
        
        # 最长休息时间设置
        self.max_break_minutes_spinbox = QSpinBox()
        self.max_break_minutes_spinbox.setRange(5, 60)
        self.max_break_minutes_spinbox.setSuffix(" 分钟")
        layout.addRow("最长休息时间:", self.max_break_minutes_spinbox)
    
    def _create_general_settings(self, parent):
        """创建通用设置
        
        Args:
            parent: 父窗口
        """
        layout = QVBoxLayout(parent)
        layout.setContentsMargins(20, 20, 20, 20)
        layout.setSpacing(15)
        
        # 创建模式设置组
        mode_group = QGroupBox("默认模式")
        mode_layout = QVBoxLayout(mode_group)
        
        self.default_mode_combo = QComboBox()
        self.default_mode_combo.addItem("番茄钟模式", "pomodoro")
        self.default_mode_combo.addItem("Flowdoro模式", "flowdoro")
        mode_layout.addWidget(self.default_mode_combo)
        
        layout.addWidget(mode_group)
        
        # 创建提醒设置组
        notification_group = QGroupBox("提醒设置")
        notification_layout = QVBoxLayout(notification_group)
        
        self.sound_enabled_checkbox = QCheckBox("启用声音提醒")
        notification_layout.addWidget(self.sound_enabled_checkbox)
        
        self.auto_start_break_checkbox = QCheckBox("自动开始休息")
        notification_layout.addWidget(self.auto_start_break_checkbox)
        
        self.auto_start_work_checkbox = QCheckBox("休息结束后自动开始工作")
        notification_layout.addWidget(self.auto_start_work_checkbox)
        
        layout.addWidget(notification_group)
        
        # 创建系统设置组
        system_group = QGroupBox("系统设置")
        system_layout = QVBoxLayout(system_group)
        
        self.start_with_system_checkbox = QCheckBox("开机自启动")
        system_layout.addWidget(self.start_with_system_checkbox)
        
        self.minimize_to_tray_checkbox = QCheckBox("最小化到托盘")
        system_layout.addWidget(self.minimize_to_tray_checkbox)
        
        layout.addWidget(system_group)
    
    def _load_config(self):
        """加载当前配置"""
        # 加载番茄钟设置
        self.work_minutes_spinbox.setValue(self.config_manager.get("pomodoro", "work_minutes"))
        self.break_minutes_spinbox.setValue(self.config_manager.get("pomodoro", "break_minutes"))
        self.long_break_minutes_spinbox.setValue(self.config_manager.get("pomodoro", "long_break_minutes"))
        self.long_break_after_spinbox.setValue(self.config_manager.get("pomodoro", "long_break_after"))
        
        # 加载Flowdoro设置
        self.break_ratio_spinbox.setValue(int(self.config_manager.get("flowdoro", "break_ratio") * 100))
        self.min_break_minutes_spinbox.setValue(self.config_manager.get("flowdoro", "min_break_minutes"))
        self.max_break_minutes_spinbox.setValue(self.config_manager.get("flowdoro", "max_break_minutes"))
        
        # 加载通用设置
        default_mode = self.config_manager.get("general", "default_mode")
        index = self.default_mode_combo.findData(default_mode)
        if index >= 0:
            self.default_mode_combo.setCurrentIndex(index)
        
        self.sound_enabled_checkbox.setChecked(self.config_manager.get("general", "sound_enabled"))
        self.auto_start_break_checkbox.setChecked(self.config_manager.get("general", "auto_start_break"))
        self.auto_start_work_checkbox.setChecked(self.config_manager.get("general", "auto_start_work"))
        self.start_with_system_checkbox.setChecked(self.config_manager.get("general", "start_with_system"))
        self.minimize_to_tray_checkbox.setChecked(self.config_manager.get("general", "minimize_to_tray"))
    
    def _save_config(self):
        """保存配置"""
        # 保存番茄钟设置
        self.config_manager.set("pomodoro", "work_minutes", self.work_minutes_spinbox.value())
        self.config_manager.set("pomodoro", "break_minutes", self.break_minutes_spinbox.value())
        self.config_manager.set("pomodoro", "long_break_minutes", self.long_break_minutes_spinbox.value())
        self.config_manager.set("pomodoro", "long_break_after", self.long_break_after_spinbox.value())
        
        # 保存Flowdoro设置
        self.config_manager.set("flowdoro", "break_ratio", self.break_ratio_spinbox.value() / 100.0)
        self.config_manager.set("flowdoro", "min_break_minutes", self.min_break_minutes_spinbox.value())
        self.config_manager.set("flowdoro", "max_break_minutes", self.max_break_minutes_spinbox.value())
        
        # 保存通用设置
        self.config_manager.set("general", "default_mode", self.default_mode_combo.currentData())
        self.config_manager.set("general", "sound_enabled", self.sound_enabled_checkbox.isChecked())
        self.config_manager.set("general", "auto_start_break", self.auto_start_break_checkbox.isChecked())
        self.config_manager.set("general", "auto_start_work", self.auto_start_work_checkbox.isChecked())
        self.config_manager.set("general", "start_with_system", self.start_with_system_checkbox.isChecked())
        self.config_manager.set("general", "minimize_to_tray", self.minimize_to_tray_checkbox.isChecked())
    
    def _on_save_clicked(self):
        """保存按钮点击事件处理"""
        self._save_config()
        self.accept()